package com.asset.dispoal.menu.service.commom;

import com.asset.dispoal.menu.domain.db.entity.GbMenuDo;
import com.asset.dispoal.menu.domain.db.mapper.GbMenuMapper;
import com.asset.dispoal.menu.dto.response.GbMenuResponse;
import com.asset.dispoal.menu.service.converter.MenuConverter;
import com.asset.dispoal.role.dto.GbRoleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/29$
 */
@Component
public class QueryRoleMenuCommand implements BaseCommand<GbRoleRequest, Result<List<GbMenuResponse>>> {


    @Autowired
    GbMenuMapper gbMenuMapper;

    @Override
    public Result<List<GbMenuResponse>> execute(GbRoleRequest request) {
        String companyId = request.getCompanyId();
        if (StringUtil.isEmpty(companyId)) {
            companyId = "1";
        }
        List<GbMenuDo> gbMenuDos = gbMenuMapper.findMenuByCompanyId(companyId);
        if (CollectionUtil.isEmpty(request.getRoleIds())) {
            return Result.faild("角色id错误");
        }
        List<String> list = gbMenuMapper.queryMenuByRole(request.getRoleIds());
        for (String id : list) {
            for (GbMenuDo gbMenuDo : gbMenuDos) {
                if (id.contains(gbMenuDo.getId())) {
                    gbMenuDo.setRoleSate("1");
                }
            }
        }
        List<GbMenuResponse> gbMenuResponses = MenuConverter.INSTANCE.doToRes(gbMenuDos);
        return MenuList.getListResult(gbMenuResponses);
    }
}

